package com.objectdraw.client.objectdraw;
import java.awt.*;

/**
 * Instantiated to store a freehand drawing object. 
 * 
 * @author jgulik, cfruehan
 *
 */
public class FreehandDrawingObject implements IDrawingObject{

	pointLink first;
	pointLink last;
	Color myColor;
	int top;
	int bottom;
	int left;
	int right;
	int anchorx=0;
	int anchory=0;
	
	/** 
	 * Constructor used to instantiate freehanddrawingobject with a point and a color
	 * 
	 * @param p used as starting point for freehand drawing
	 * @param c color of freehand drawing
	 */
	public FreehandDrawingObject(Point p, Color c){
		right=p.x;
		left=p.x;
		top=p.y;
		bottom=p.y;
		first=new pointLink(p);
		last=first;
		myColor=c;
		//System.out.println("made new fdo");
	}
	
	/**
	 * Overloaded contructer that uses strings instead of point and color objects
	 * 
	 * @param x1 becomes x coord of starting point
	 * @param y1 becomes y coord of starting point
	 * @param color becomes color of freehand drawing objec
	 */
	public FreehandDrawingObject(String x1, String y1, String color){
		
		if(color.equalsIgnoreCase("red")){
			myColor = Color.RED;
		}else if(color.equalsIgnoreCase("blue")){
			myColor = Color.BLUE;
		}else if(color.equalsIgnoreCase("green")){
			myColor = Color.GREEN;
		}else{
			myColor = Color.BLACK;
		}
		
		
		
		
		right=Integer.parseInt(x1);
		left=Integer.parseInt(x1);
		top=Integer.parseInt(y1);
		bottom=Integer.parseInt(y1);
		
		first=new pointLink(new Point(Integer.parseInt(x1),Integer.parseInt(y1)));
		last=first;

	}
	
    /**
     * draws this object to the canvas
     */
	public void drawMe(Graphics g) {	
		//System.out.println("draw me");
		//Color tempColor = g.getColor();
		g.setColor(myColor);
		pointLink current=first;
		while(current.next!=null){
			g.drawLine(current.p.x+anchorx,current.p.y+anchory,current.next.p.x+anchorx, current.next.p.y+anchory);
			current=current.next;
		}
		//g.setColor(tempColor);
	}
	
	/**
	 * checks if point is in the bounding box of the object
	 */
	public boolean isPointInObject(Point p){
		return p.x<=right+anchorx && p.x>=left+anchorx && p.y>=top+anchory && p.y<=bottom+anchory;
	}
	
	/** 
	 * add point to freehand drawing object
	 * @param p next point in freehand drawing object
	 */
	public void add(Point p){
		if(p.x>right){ right=p.x;}
		if(p.y<top){ top=p.y;}
		if(p.x<left){ left=p.x;}
		if(p.y>bottom){ bottom=p.y;}
		
		last.next=new pointLink(p);
		last=last.next;
	}

	/**
	 * return height of bounding box
	 */
	public int getHeight() {
		return bottom-top;
	}

	/**
	 * return top of bounding box
	 */
	public int getTop() {
		return top+anchory;
	}

	/**
	 * returns width of bounding box
	 */
	public int getWidth() {
		return right-left;
	}
	
	/**
	 * return the left side of the bounding box
	 */
	public int getLeft(){
		return left+anchorx;
	}
	
	/**
	 * set color of the object to c
	 */
	public void setmycolor(Color c){
		myColor=c;
	}

	/**
	 * move the anchor point by (+x, +y)
	 */
	public void moveAnchor(int x, int y) {
		anchorx+=x;
		anchory+=y;
	}
}

class pointLink{
	public pointLink(Point p){
		this.p=p;
		this.next=null;
	}
	public Point p;
	public pointLink next;
}